/*
 * @Descripttion: 
 * @version: 
 * @Author: cyj
 * @Date: 2023-04-17 20:51:55
 */
//导入数据库实例 便于操作数据库
const db = require('../config/db')

//搜索用户
exports.searchUser = (req, res) => {
    //获取前端传入的搜索关键词
    let keyword = req.query.keyword
    let uid = req.query.uid
    //先查找用户
    let sql = `select * from user where username like ? `
    //查询包含keyword关键字的用户名
    db.query(sql, "%" + keyword + "%", (err, result) => {
        if (err) return res.sendMsg(err, 400)
        if (result.length == 0) { //如果用户名搜索不到，则就搜索邮箱
            let str = 'select * from user where email like ?'
            db.query(str, '%' + keyword + '%', (err, result1) => {
                if (err) return res.sendMsg(err, 400)
                if (result1.length == 0) return res.sendMsg('搜索数据不存在!', 400)
                else {
                    let newRes1 = [];
                    for (let i = 0; i < result1.length; i++) {
                        //过滤掉自己
                        if(result1[i].userid == uid) continue;
                        //剔除密码
                        let temp = {
                            userid: result1[i].userid,
                            username: result1[i].username,
                            email: result1[i].email,
                            birth: result1[i].birth,
                            explain: result1[i].explain,
                            imgurl: result1[i].imgurl,
                            phone: result1[i].phone,
                            registerTime: result1[i].registerTime,
                            sex: result1[i].sex,
                        }
                        newRes1.push(temp)
                    }
                    res.sendMsg('success', 200, newRes1)
                }
            })
        }
        else { //如果用户名搜到结果，就返回
            // console.log(result);
            let newRes = [];
            for (let i = 0; i < result.length; i++) {
                //剔除密码
                let temp = {
                    userid: result[i].userid,
                    username: result[i].username,
                    email: result[i].email,
                    birth: result[i].birth,
                    explain: result[i].explain,
                    imgurl: result[i].imgurl,
                    phone: result[i].phone,
                    registerTime: result[i].registerTime,
                    sex: result[i].sex,
                }
                newRes.push(temp)
            }
            res.sendMsg('success', 200, newRes)
        }
    })
}

//判断是否是好友
exports.isFriend = (uid, fid, res) => {
    let sql = 'select * from friend where userid = ? and friendid = ? and state = 0 '
    db.query(sql, [uid, fid], (err, result) => {
        if (err) return res.sendMsg(err, 500)
        if (result.length == 0) return res.sendMsg('不是好友', 400)
        else {
            res.sendMsg('是好友', 200, result)
        }
    })
}

//搜索群 搜索群名
exports.searchGroup = (req, res) => {
    let name = req.query.name
    let sql = 'select groupname,imgurl from `group` where groupname = ?'
    db.query(sql, name, (err, result) => {
        if (err) return res.sendMsg(err, 400)
        if (result.length == 0) return res.sendMsg('群不存在', 400)
        else {
            res.sendMsg('success', 200, result)
        }
    })
}

//判断是否在群内
exports.isInGroup = (uid, gid, res) => {
    let sql = 'select * from groupuser where userid = ? and groupid = ?'
    db.query(sql, [uid, gid], (err, result) => {
        if (err) return res.sendMsg(err, 400)
        if (result.length == 0) return res.sendMsg('当前用户不在群内', 400)
        else {
            res.sendMsg('该用户在群内', 200, result)
        }
    })
}